#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //创建菜单栏
    QMenuBar* menubar = this->menuBar();
    this->setMenuBar(menubar);

//    QMenuBar* menubar1 = new QMenuBar(this);
//    this->setMenuBar(menubar1);

    //创建菜单
    QMenu* menu1 = new QMenu("文件 (&F)");
    QMenu* menu2 = new QMenu("编辑 (&E)");
    QMenu* menu3 = new QMenu("菜单1 (&B)");
    //创建子菜单
    QMenu* menu4 = new QMenu("子菜单1");
    QMenu* menu5 = new QMenu("子菜单2");
    //添加到菜单栏中
    menubar->addMenu(menu1);
    menubar->addMenu(menu2);
    menubar->addMenu(menu3);
    //子菜单添加到菜单中
    menu3->addMenu(menu4);
    menu3->addMenu(menu5);

    //创建菜单选项
    QAction* action1 = new QAction("新建");
    QAction* action2 = new QAction("退出");
    QAction* action3 = new QAction("Undo");
    QAction* action4 = new QAction("Redo");
    QAction* action5 = new QAction("菜单项1");
    QAction* action6 = new QAction("菜单项2");
    QAction* action7 = new QAction("菜单项3");
    QAction* action8 = new QAction("菜单项4");
    QAction* action9 = new QAction("菜单项5");
    QAction* action10 = new QAction("菜单项6");

    //将菜单选项添加到菜单中
    menu1->addAction(action1);
    menu1->addAction(action2);
    menu2->addAction(action3);
    menu2->addAction(action4);
    menu3->addAction(action5);
    menu3->addAction(action6);
    //将菜单选项添加到子菜单中
    menu4->addAction(action7);
    menu4->addSeparator();    //添加分割线
    menu4->addAction(action8);
    menu5->addAction(action9);
    menu5->addAction(action10);


    //添加action对应的槽函数
    connect(action1, &QAction::triggered, this, &MainWindow::handler);
    connect(action2, &QAction::triggered, this, &MainWindow::close);  //MainWindow自带的close关闭函数

    //添加图标
    menu1->setIcon(QIcon(":/people.png"));
    menu4->setIcon(QIcon(":/people.png"));
    action7->setIcon(QIcon(":/people.png"));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handler()
{
    qDebug() << "新建操作被触发了";
}

